<template>
<div class="dashboard-container">
  <div class="op-form">
    <el-button icon="el-icon-refresh" @click="fetchData" size="small" type="primary" round>刷新</el-button>
  </div>
  <el-row :gutter="gutter" class="card-line"> 
    <el-col :span="span6">
      <el-card shadow="always">
        <div class="card-title">
          <span><svg-icon icon-class="user" /> 总用户数</span>
        </div>
        <div class="card-op-board">
          <div class="info">
            <el-tag>{{totalUserCount}}</el-tag>
          </div>
          <div class="opBtn">
            <el-button type="primary" size="mini" class="button" @click="goUser" round>进入用户管理</el-button>
          </div>
          <div class="clear"></div>
        </div>
      </el-card>
    </el-col>
    <el-col :span="span6">
      <el-card shadow="always">
        <div class="card-title" style="background-color:#FD6833;">
          <span><svg-icon icon-class="today" /> 今日新增用户数</span>
        </div>
        <div class="card-op-board">
          <div class="info">
            <el-tag>{{todayUserCount}}</el-tag>
          </div>
          <div class="opBtn">
            <el-button type="primary" size="mini" class="button" @click="goUser" round>进入用户管理</el-button>
          </div>
          <div class="clear"></div>
        </div>
      </el-card>
    </el-col>
    <el-col :span="span6">
      <el-card shadow="always">
        <div class="card-title" style="background-color:#54CF97;">
          <span><svg-icon icon-class="file" /> 文件总阅读数</span>
        </div>
        <div class="card-op-board">
          <div class="info">
            <el-tag>{{totalFReadCount}}</el-tag>
          </div>
          <div class="opBtn">
            <el-button type="primary" size="mini" class="button" @click="goFile" round>进入文件管理</el-button>
          </div>
          <div class="clear"></div>
        </div>
      </el-card>
    </el-col>
    <el-col :span="span6">
      <el-card shadow="always">
        <div class="card-title" style="background-color:#FF4301;">
          <span><svg-icon icon-class="download" /> 文件总下载数</span>
        </div>
        <div class="card-op-board">
          <div class="info">
            <el-tag>{{totalFDwCount}}</el-tag>
          </div>
          <div class="opBtn">
            <el-button type="primary" size="mini" class="button" @click="goFile" round>进入文件管理</el-button>
          </div>
          <div class="clear"></div>
        </div>
      </el-card>
    </el-col>
  </el-row>
  <!-- 控制面板 -->
  <el-row :gutter="gutter" class="card-line"> 
    <el-col :span="span6">
      <el-card shadow="always">
        <div class="card-title" style="background-color:#304156;">
          <span><svg-icon icon-class="user" /> 微信客服号</span>
        </div>
        <div class="card-op-board">
          <div class="info">
            <el-input size="mini" placeholder="请输入内容" v-model="wxHelpNo"></el-input>
          </div>
          <div class="opBtn">
            <el-button size="mini" class="button" @click="upSetting(2)">更新</el-button>
          </div>
          <div class="clear"></div>
        </div>
      </el-card>
    </el-col>
    <el-col :span="span6">
      <el-card shadow="always">
        <div class="card-title" style="background-color:#304156;">
          <span><svg-icon icon-class="max_count" /> 日文件下载数</span>
        </div>
        <div class="card-op-board">
          <div class="opBtn">
            <el-input-number size="mini" v-model="dayFileDwCountLmt" :min="1" :max="100" label="日文件下载数"></el-input-number>
            <el-button size="mini" @click="upSetting(3)">更改</el-button>
          </div>
          <div class="clear"></div>
        </div>
      </el-card>
    </el-col>
    <el-col :span="span6">
      <el-card shadow="always">
        <div class="card-title" style="background-color:#304156;">
          <span><svg-icon icon-class="checking" /> 提审开关</span>
        </div>
        <div class="card-op-board">
          <div class="opBtn">
            <el-switch style="height: 28px;"
              v-model="inChecking"
              active-color="#13ce66"
              inactive-color="#ff4949" @change="upSetting(1)">
            </el-switch>
          </div>
          <div class="clear"></div>
        </div>
      </el-card>
    </el-col>
  </el-row>
  <!-- 排行 -->
  <el-row :gutter="gutter" class="card-line"> 
    <el-col :span="span10">
      <!-- 日文件阅读排行 -->
      <el-card shadow="always">
        <div class="card-title" style="background-color:#FF4301;">
          <span><svg-icon icon-class="today" /> 日文件阅读排行</span>
        </div>
        <div class="card-op-board" style="padding:10px;">
          <!-- 排行主体 -->
          <el-table
            :data="todayFrRank" 
            v-loading="loading" 
            height="250" 
            stripe
            style="width: 100%">
            <el-table-column
              prop="idx"
              label="排名"
              width="80">
            </el-table-column>
            <el-table-column
              prop="name"
              label="文件"
              width="300">
            </el-table-column>
            <el-table-column
              prop="count"
              label="阅读数">
            </el-table-column>
          </el-table>
        </div>
      </el-card>
    </el-col>
    <el-col :span="span10">
      <!-- 文件阅读排行 -->
      <!-- 日文件阅读排行 -->
      <el-card shadow="always">
        <div class="card-title" style="background-color:#54CF97;">
          <span><svg-icon icon-class="rank" /> 总文件阅读排行</span>
        </div>
        <div class="card-op-board" style="padding:10px;">
           <!-- 排行主体 -->
          <el-table
            :data="totalFrRank" 
            v-loading="loading" 
            height="250" 
            stripe
            style="width: 100%">
            <el-table-column
              prop="idx"
              label="排名"
              width="80">
            </el-table-column>
            <el-table-column
              prop="name"
              label="文件"
              width="300">
            </el-table-column>
            <el-table-column
              prop="count"
              label="阅读数">
            </el-table-column>
          </el-table>
        </div>
      </el-card>
    </el-col>
  </el-row>
</div>
</template>

<script>
import { index, upSysSetting } from '@/api/dashboard'
export default {
  name: 'dashboard',
  data() {
    return {
      span6: 6,
      span10: 10,
      gutter: 20,
      loading: false,
      todayUserCount: 0,
      totalUserCount: 0,
      totalFReadCount: 0,
      totalFDwCount: 0,
      wxHelpNo: '',
      dayFileDwCountLmt: 0,
      inChecking: false,
      todayFrRank: [],  // 今日阅读榜单
      totalFrRank: []   // 阅读总榜
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.loading = true
      index().then(res => {
        this.loading = false
        this.todayUserCount = res.todayUserCount
        this.totalUserCount = res.totalUserCount
        this.totalFReadCount = res.totalFReadCount
        this.totalFDwCount = res.totalFDwCount
        this.wxHelpNo = res.wxHelpNo
        this.dayFileDwCountLmt = res.dayFileDwCountLmt
        // 是否在提审中
        if (res.inChecking == 1) {
          this.inChecking = true
        } else {
          this.inChecking = false
        }
        this.todayFrRank = res.todayFrRank
        this.totalFrRank = res.totalFrRank
      }).catch(err => {
        this.loading = false
      })
    },
    goUser() {
      this.$router.push({path: '/user/index'})
    },
    goFile() {
      this.$router.push({path: '/report/index'})
    },
    upSetting(id) {
      let value = undefined
      switch(id) {
        case 1: 
          console.log(this.inChecking)
          if (this.inChecking == false) {
            value = 0
          } else {
            value = 1
          }
          break;
        case 2:
          value = this.wxHelpNo
          break;
        case 3:
          value = this.dayFileDwCountLmt
          break;
      }
      if (value !== undefined) {
        // 更新系统设置
        upSysSetting(id, value).then(res => {
          this.$message.success('更新设置成功')
        })
      }
      
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.dashboard-container {
  width: 1224px;

  .op-form {
    background: #F5F9FF;
    border: 1px dashed #CCCCCC;
    border-radius: 5px;
    padding: 10px;
    margin-bottom: 12px;
  }

}
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
.card-line {
  margin-bottom: 30px;
}
.card-title {
    background-color: #409EFF;
    color: #FFFFFF;
    width: 100%;
    height: 50px;
    font-weight: bold;
    line-height: 40px;
    box-sizing: border-box;
    padding: 5px 10px 10px 10px;
    box-shadow: 0px 2px 2px #CCCCCC;
  }
  .card-op-board {
    margin-top: 10px;
    .info {
      float: left;
      padding-left: 10px;
      line-height: 25px;
      // font-weight: bold;
    }
    .opBtn {
      float: right;
      padding-right: 10px;
    }
    .clear {
      clear: both;
    }
  }

</style>
